package com.memexue.order.service.impl;

import com.memexue.common.DecreaseStockInput;
import com.memexue.common.ProductInfoOutput;
import com.memexue.order.dto.OrderDto;
import com.memexue.order.enums.OrderStatusEnum;
import com.memexue.order.enums.PayStatusEnum;
import com.memexue.order.po.OrderDetail;
import com.memexue.order.po.OrderMaster;
import com.memexue.order.repository.OrderDetailRepository;
import com.memexue.order.repository.OrderMasterRepository;
import com.memexue.order.service.OrderService;
import com.memexue.order.util.KeyUtils;
import com.memexue.product.client.ProductClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDetailRepository orderDetailRepository;
    @Autowired
    private OrderMasterRepository orderMasterRepository;
    @Autowired
    private ProductClient productClient;

    @Override
    @Transactional
    public OrderDto create(OrderDto orderDto) {
        String orderId = KeyUtils.genUniqueKey();
        // 查询商品信息（调商品服务）
        List<String> productIdList = orderDto.getOrderDetailList().stream()
                .map(OrderDetail::getProductId)
                .collect(Collectors.toList());
        List<ProductInfoOutput> productInfoList = productClient.listForOrder(productIdList);
        // 计算总价
        BigDecimal orderAmount = new BigDecimal(BigInteger.ZERO);
        for(OrderDetail orderDetail : orderDto.getOrderDetailList()){
            for(ProductInfoOutput productInfo : productInfoList){
                if(orderDetail.getProductId().equalsIgnoreCase(productInfo.getProductId())){
                    // 单价*数量
                    orderAmount = orderAmount.add(productInfo.getProductPrice()
                            .multiply(new BigDecimal(orderDetail.getProductQuantity())));
                    BeanUtils.copyProperties(productInfo, orderDetail);
                    orderDetail.setOrderId(orderId);
                    orderDetail.setDetailId(KeyUtils.genUniqueKey());
                }
            }
        }
        // 扣库存（调用商品服务）
        List<DecreaseStockInput> cartDtoList = orderDto.getOrderDetailList().stream()
                .map(e -> new DecreaseStockInput(e.getProductId(), e.getProductQuantity()))
                .collect(Collectors.toList());
        productClient.decreaseStock(cartDtoList);
        // 5. 订单入库
        OrderMaster orderMaster = new OrderMaster();
        BeanUtils.copyProperties(orderDto, orderMaster);
        orderMaster.setOrderId(orderId);
        orderMaster.setOrderAmount(orderAmount);
        orderMaster.setOrderStatus(OrderStatusEnum.NEW.getCode());
        orderMaster.setPayStatus(PayStatusEnum.UNPAY.getCode());
        orderMasterRepository.save(orderMaster);
        // 订单详情入库
        for(OrderDetail orderDetail : orderDto.getOrderDetailList()){
            orderDetailRepository.save(orderDetail);
        }
        return orderDto;
    }
}
